<?php
 include "counter2.php";

pagestart();
print "<html>";
pagehead(_("Operation statistics"));
print "\n<BODY bgcolor=\"white\">\n";
pagetop();
print "\n<h1>" . _("Operation counts against the counter tables") . "</h1>\n" .

_("These statistics are generated from the counter log.") .
"\n<p>\n" .
_("Operations against the persons table affect the person count and statistics.") . "\n<br>\n" .
_("Operations against the machine table affect the machine count and statistics.") . "\n<br>\n" .
_("Operations on the users table are logins, and most other updates.") . "\n<br>\n" . 
_("Operations on the frozen table indicate users being frozen or thawed.") .
"\n<p>\n";

	$days = 30;
	$firstday = strftime("%Y-%m-%d 00:00:00",
				time() - 60*60*24*$days);
	$lastday = strftime("%Y-%m-%d 23:59:59",
				time());
	$tables = array("persons", "machines", "users", "frozen");

print _("The statistics are from ") . $firstday . _(" to ") . $lastday . ".";

	$sql = "select substring(date, 1, 10) as m, count(*), tablename, operation from f_log where date > '$firstday' and date < '$lastday' group by m, tablename, operation order by m desc";

        $placeq = mysql_query($sql);
        while ( $place = mysql_fetch_array($placeq)) {
	    $ops["Sum"][$place[2]][$place[3]] += $place[1];
	    $ops[$place[0]][$place[2]][$place[3]] = $place[1];
//	    print("<tr><td>" . $place[0] . "<td>" . $place[1]
//	    . "<td>" . $place[2] . "<td>" . $place[3] . "\n");
//	    $tables[$place[2]] = 1;
        }

print "\n<table border>\n";

	print "\n<tr><th>" . _("Date") . "</th>";
        foreach ($tables as $table) {
            print "\n<th colspan=3>" . $table . "</th>";
	}
	print "</tr>\n<tr><th></th>";
        foreach ($tables as $table => $nonsense) {
            print "\n<th>" . _("New") . "</th><th>" . _("Update") . "</th><th>" . _("Delete") . "</th>";
	}
	print "\n</tr>";
	// Amazingly, php returns entries in CREATION TIME order....
	// so we don't need to sort the entries here.
	foreach ($ops as $date => $arr) {
	    print "\n<tr><td>$date</td>";
	    foreach ($tables as $table) {
	        foreach (array("new", "update", "delete") as $op) {
		    print "<td>" . $ops[$date][$table][$op] . "</td>";
		}
            }
            print "</tr>\n";
        }
        print "\n</tr>" .
"\n</table>\n";
pagebottom();
print "\n</body>\n</html>";
?>
